package com.xinxing.learning.crud.batch.common;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorConfig {
    private static final int maxPoolSize = Runtime.getRuntime().availableProcessors();
    private volatile static ExecutorService executorService;

    public static ExecutorService getThreadPool() {
        if (executorService == null) {
            synchronized (ExecutorConfig.class) {
                if (executorService == null) {
                    executorService = newThreadPool();
                }
            }
        }
        return executorService;
    }

    private static ExecutorService newThreadPool() {
        int queueSize = 500;
        int corePool = Math.max(5, maxPoolSize);
        return new ThreadPoolExecutor(corePool,
                maxPoolSize,
                10000L,
                TimeUnit.MICROSECONDS,
                new LinkedBlockingDeque<>(queueSize),
                new ThreadPoolExecutor.AbortPolicy());
    }
}
